import { objDeepCopy,getCascaderObj ,getDateString} from "@/assets/js/common.js";
let Controller = {
	created() {
		this.init();
	},
	mounted() {
		
	},
	methods: {
		init() {
			
		},
		//扫码入库 请求()
		codeEntryAjax(that,code){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'get',
					url:apiTest+'/supplier/whWarehouseDetail/getGoodsInfo?batchNo='+code,
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						var obj = {};
						var result = data.result;
						if(result){
							obj = objDeepCopy(result);
							obj.mainQuantity = 1;
							if(result.batchNo){
								obj.isBatchNo = true;
							}else{
								obj.isBatchNo = false;
							}
							that.Model.tableData.push(obj)
						}else{
							this.tips(this,'warning','未找到相关条码');
						}
					}else{
						this.tips(this,'warning',data.msg);
					}
			
				});
		},
		
		//扫码入库
		codeEntry(){
			var code = this.Model.searchCode;
			if(code.length == 0){
				this.tips(this,'warning','请输入条码或者商品编码');
				return;
			}
			var tableData = this.Model.tableData;
			var codeIndexOf = this.valueInData(code,tableData);
			if(codeIndexOf == -1){
				this.codeEntryAjax(this,code);
			}else{
				this.tips(this,'warning','条码已存在')
			}
		},
		//遍历 判断是否存在数组中
		valueInData(val,data){
			var indexof = -1;
			data.forEach(function(value,index,array){
				Object.keys(value).forEach(function(key){
					// console.log(key,value[key]);
					if(key == 'batchNo' && value[key] == val){
						indexof = 0;
						return;
					}
				});
			})
			return indexof;
		},
		//删除 表格数据
		deleteTableData(){
			var that = this;
			var TableSelectData = this.Model.TableSelectData;
			var tableData = this.Model.tableData;
			if(TableSelectData.length == 0){
				this.tips(this,'warning','请先勾选需要删除的数据');
				return;
			}
			this.$confirm('此操作将删除相应数据, 是否继续?', '提示', {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消',
			}).then(() => {
				TableSelectData.forEach(function(value,index,array){
					tableData.forEach(function(val,idx,arr){
						if(value == val){
							that.Model.tableData.splice(idx,1);
						}
					})
				})
				this.$message({
					type: 'success',
					message: '删除成功!'
				});
			}).catch(() => {
			           
			});
		},
		//点击 提交调拨单
		onConfirmNew(){
			var arr = [];
			var obj = {};
			var tableData = this.Model.tableData;
			for (let i = 0; i < tableData.length; i++) {
				obj = {
					// id:tableData[i].id,
					barCode:tableData[i].batchNo,
					mainQuantity:tableData[i].mainQuantity,
				}
				arr.push(obj);
			}
			if(tableData.length == 0){
				this.tips(this,'warning','请先扫码或输入数据');
				return;
			}
			//提交的数据 formInline
			var formInline = this.Model.formInline;
			formInline.originId = this.Model.originId;
			formInline.whWarehouseDetails = arr;
			this.$confirm('此操作将提交新建订单, 是否继续?', '提示', {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消',
			}).then(() => {
				this.confirmNewAjax(this,formInline);
			}).catch(() => {
			    
			});
			
		},
		//提交新建 请求
		confirmNewAjax(that,params){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					url:apiTest+'/supplier/whWarehouse/addAuthInBill',
					data:params
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						var result = data.result;
						if(result == 1){
							that.tips(that,'success','操作成功')
						}
					}else{
						that.tips(that,'warning',data.msg)
					}
			
				});
		},
		//点击导入物料按钮
		importMaterial(){
			this.Model.importFileDialog = true;
		},
		//选择文件
		fileChange(){
			var fileObj = document.getElementById("fileup").files[0];
			var form = new FormData();
			form.append("file", fileObj);
			this.Model.upFileForm = form;
			var that = this;
			var obj = document.getElementById("fileup");
			var len = obj.files.length;
			for (var i = 0; i < len; i++) {
				var temp = obj.files[i].name;
				that.Model.fileupName = temp;
			}
		},
		//点击上传
		uploadExcel(){
			var apiTest = this.apiTest;
			var tableData = objDeepCopy(this.Model.tableData);
			var that = this;
			this.$ajax({
				method: 'post',
				url:apiTest+'/supplier/whWarehouseDetail/importExcelList',
				data:that.Model.upFileForm
			}).then((res)=>{
				console.log(res);
				var obj = {};
				if(res.data.status == 200){
					var result = res.data.result;
					if(result){
						for (let i = 0; i < result.length; i++) {
							var indexof = -1;
							obj = objDeepCopy(result[i]);
							if(tableData.length == 0){
								that.Model.tableData.push(obj);
							}else {
								tableData.forEach(function(value,index,array){
									if(value.batchNo == obj.batchNo){
										indexof = 0;
									}
								})
								if(indexof == -1){
									that.Model.tableData.push(obj);
								}
							}
							
						}
					}
					// that.tips(that,'success','导入成功！')
					setTimeout(function(){
						that.Model.importFileDialog = false;
					},1000);
				}else{
					that.tips(that,'warning','导入失败！')
				}
			});
		},
		//下载模板
		downloadExcel(){
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url:apiTest+'/supplier/whWarehouseDetail/getGoodsExcelInfo',
			}).then((res)=>{
				console.log(res);
				if(res.data.status == 200){
					var result = res.data.result;
					if(result.fileUrl){
						window.open(result.fileUrl);
					}
					
				}else{
					
				}
			});
		},
		//搜索 更多
		searchMore(){
			var no = this.Model.number;
			this.getMsgAjax(this,no)
		},
		//根据调拨单号查询
		getMsgAjax(that,number){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					url:apiTest+'/supplier/whWarehouse/getEntity',
					data:{
						number:number
					}
			    }).then((res)=>{
					console.log(res);
					// that.Model.getTypeOptionData = [];
					var data = res.data;
					if(data.status == 200){
						var obj = {};
						var result = data.result;
						if(result){
							if(result.businessType == 130){
								this.Model.messageShow = true;
								/* tabFormInline */
								that.Model.originId = result.id;
								that.Model.tabFormInline = [
									{
										label:'认证机构',
										value:result.authenticationOrganization
									},
									{
										label:'认证类型',
										value:result.authenticationTyle
									},
								]
							}else{
								that.tips(that,'warning','单号不是认证出库单');
							}
							
							
						}else{
							that.tips(that,'warning','未查询到相关单号');
							this.Model.messageShow = false;
						}
						
					}else{
			
					}
			
				});
		},
		//返回上一页
		back(){
			this.$router.push({
				path:'certificationStorageList',
				query:{
					
				}
			});
		},
		//确认 入库
		onConfirmStorage(){
			var arr = [];
			var obj = {};
			var tableData = this.Model.tableData;
			for (let i = 0; i < tableData.length; i++) {
				obj = {
					// id:tableData[i].id,
					barCode:tableData[i].batchNo,
					mainQuantity:tableData[i].mainQuantity,
				}
				arr.push(obj);
			}
			if(tableData.length == 0){
				this.tips(this,'warning','请先扫码或输入数据');
				return;
			}
			//提交的数据 formInline
			var formInline = this.Model.formInline;
			formInline.id = this.Model.originId
			formInline.whWarehouseDetails = arr;
			this.$confirm('此操作将提交保存新建订单, 是否继续?', '提示', {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消',
			}).then(() => {
				this.confirmOutAjax(this,formInline);
			}).catch(() => {
			    
			});
		},
		//确定出库 请求
		confirmOutAjax(that,params){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					url:apiTest+'/supplier/whWarehouse/confirmAuthIn',
					data:params
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						var result = data.result;
						if(result == 1){
							that.tips(that,'success','操作成功')
						}
					}else{
						that.tips(that,'warning',data.msg)
					}
			
				});
		},
		//取消订单
		cancelOrder() {
			
		},
		showNotSureBarCodeP1() {
			
		},
		showNotSureBarCodeP2(){
			
		},
		toSearchNotSureBarCode() {
			
		},
		//
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		tips(that,type,message) {
		  that.$message({
		    message: message,
		    type: type
		  });
		},
		toggleTableDetail(val) {
			const obj = this.Model[val];
			if (obj.flag === true) {
				this.Model[val].src = require('@/assets/img/icon_collapse.png');
				this.Model[val].flag = false;
			} else {
				this.Model[val].src = require('@/assets/img/icon_unfold.png');
				this.Model[val].flag = true;
			}
		},
		handleSelectionChange(val) {
		  this.Model.TableSelectData = val;
		},
		handleCurrentChange(val) {
		  this.Model.pager.params.pageIndex = val;
		},
		handleSizeChange(val) {
		  this.Model.pager.params.pageSize = val;
		},
		cardClick(color) {
		  this.Model.cardSelected = color;
		},
		showChild(val) {
		  this.Model.searchFromData = val;
		},
		toSearch(val) {
			var params = {
					conditcion:this.Model.searchFromData
				}
			this.getCommodity(this,params)
		},
		toClear(val) {
		  this.Model.searchFromData = this.Model.defaultSearchFromData;
		},
		toExport(val) {
		  console.log(val);
		},
		
		
		//遍历 得到label及value
		getCascaderObj(val,opt){
			return val.map(function (value, index, array) {
                for (var item of opt) {
                    if (item.code == value) {
						opt = item.children;
						return item;
					}
                }
                return null;
            });
		},
		//合计
		getSummaries(param) {
		var that = this;
        const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '总价';
            return;
          }else if(index === 5 || index === 6) {
			  const values = data.map(item => Number(item[column.property]));
			  sums[index] = values.reduce((prev, curr) => {
			    const value = Number(curr);
			    if (!isNaN(value)) {
			      return prev + curr;
			    } else {
			      return prev;
			    }
			  }, 0);
			  if(index === 6){
				that.Model.totlePrice = sums[index];
			  }else{
				
			  }
			  
		  }else{
			sums[index] = '/';
		  }
        });

        return sums;
      }
  }
}
export default Controller;
